//
//  EYMethod.m
//  metadata
//
//  Created by easy on 11-5-27.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "EYMethod.h"


@implementation EYMethod
@synthesize ownMethod = own;
@synthesize name;
@synthesize numberOfArguments;
@synthesize returnType;
@synthesize typeEncoding;
@synthesize implementation;

-(id) initWith:(Method) method{
	if (self = [super init]) {
		own = method;
	}
	return self;
}
+(id) init:(Method) method{
	return [[[EYMethod alloc] initWith:method] autorelease];
}

-(void) dealloc{
	own = nil;
	[super dealloc];
}

-(NSString *) argumentTypeAtIndex:(NSUInteger) index{
	char rtn[METHOD_RETURN_TYPE_SIZE];
	method_getArgumentType(self.ownMethod, index, rtn, METHOD_RETURN_TYPE_SIZE);
	return [NSString stringWithCString:rtn encoding:DEFAULT_ENCODING];
}
-(NSString *) copyArgumentTypeAtIndex:(NSUInteger) index{
	return [[NSString alloc] initWithCString:method_copyArgumentType(self.ownMethod, index) encoding:DEFAULT_ENCODING];
}
-(NSString *) copyReturnType{
	return [[NSString alloc] initWithCString:method_copyReturnType(self.ownMethod) encoding:DEFAULT_ENCODING];
}
+(void) exchange:(EYMethod *) method1 with:(EYMethod *) method2{
	method_exchangeImplementations(method1.ownMethod, method2.ownMethod);
}
-(void) exchangeWith:(EYMethod *) method{
	[EYMethod exchange:self with:method];
}
#pragma mark -
#pragma mark setter/getter 

-(void) setImplementation:(IMP) imp{ 
	method_setImplementation(self.ownMethod, imp);
}
-(IMP) implementation{
	return method_getImplementation(self.ownMethod);
}

-(EYSelector *) name{
	return [EYSelector init:method_getName(self.ownMethod)];
}
-(int) numberOfArguments{
	return method_getNumberOfArguments(self.ownMethod);
}
-(NSString *) returnType{
	char rtn[METHOD_RETURN_TYPE_SIZE];
	method_getReturnType(self.ownMethod, rtn, METHOD_RETURN_TYPE_SIZE);
	return [NSString stringWithCString:rtn encoding:DEFAULT_ENCODING];
}
-(NSString *) typeEncoding{
	return [NSString stringWithCString:method_getTypeEncoding(self.ownMethod) encoding:DEFAULT_ENCODING];
}
@end
